; NUC-KP4.INS - KayPro 4-84 & KayPro 10 - 04/21/86
;
; This insert is for the Kaypro standard internal clock.
;
; Note: This is an insert, not an overlay.
;
; ========
; 04/21/86  Modified for NUBYE
; 07/15/86  First version based upon code found in TIMEx.ASM - Wayne Masters
; ========
;
; Use the program TIMEx.ASM to initialize your clock with correct date/time
; prior to running NUBYE.
;
;
CENTURY	EQU	19H		; 19h = 19 bcd, reset this every 100 years
CYEAR	EQU	85H		; 85h = 85 bcd, reset this every year
RTCA	EQU	20H		; Clock address select reg
RTCD	EQU	24H		; Clock data register
RTCS	EQU	22H		; Clock status register
REGEND	EQU	8		; Ending register count for time loop
;
TIME:	MVI	A,0CFH		; Initial status setup byte
	OUT	RTCS		; Set pio for mode 3 in/output
	MVI	A,0E0H		; Low 5 bits output, top 3 input
	OUT	RTCS		; Set pio in/out bits
	MVI	A,3		; Disable interrupts
	OUT	RTCS		; Do it
	MVI	A,14H		; Status reg addr
	OUT	RTCA		; Select it
	IN	RTCD		; Reset status bit
;
DOREAD:	LXI	H,LOCBUF	; Point to time save area
	MVI	B,2		; Start with seconds
;
BURST:	MOV	A,B		; A is register we want to read
	CPI	REGEND		; Gotten all we want?
	JNC	ROLLCK		; Yes, done getting time
	OUT	RTCA		; Select that register of clock
	IN	RTCD		; Read the clock data
	MOV	M,A		; Save in core
	INX	H		; Next memory location
	INR	B		; Next reg addr
	JMP	BURST		; Go get more data
;
; See if the clock rolled over during the reads.
;
ROLLCK:	MVI	A,14H		; Status reg addr
	OUT	RTCA		; Select it
	IN	RTCD		; Get status
	ORA	A		; Was clock roll?
	JNZ	DOREAD		; Yes, go read again
;
; Format the date and time for bye's realtime clock buffer
;
	LDA	LOCBUF		; BCD seconds
	STA	RTCBUF+2
	LDA	LOCBUF+1	; BCD minutes
	STA	RTCBUF+1
	CALL	BCDBIN		; Convert to binary
	STA	CCMIN		; For NUBYE
	LDA	LOCBUF+2	; BCD hours
	STA	RTCBUF
	CALL	BCDBIN		; Convert to binary
	STA	CCHOUR		; For NUBYE
	LDA	LOCBUF+4	; BCD day of month
	STA	RTCBUF+6
	LDA	LOCBUF+5	; BCD month
	STA	RTCBUF+5
	MVI	A,CENTURY
	STA	RTCBUF+3
	MVI	A,CYEAR
	STA	RTCBUF+4
	RET			; All done
;
LOCBUF:	DB	0,0,0,0,0,0	; Seconds, Minutes, Hours,
 				; Day of weekk, Day of month, Month of year
;
; end of insert
; -------------